package com.example.ymm.modules1.service.dd;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiProcessinstanceGetRequest;
import com.dingtalk.api.request.OapiProcessinstanceListidsRequest;
import com.dingtalk.api.response.OapiProcessinstanceGetResponse;
import com.dingtalk.api.response.OapiProcessinstanceListidsResponse;
import com.example.ymm.exception.YmException;
import com.example.ymm.util.DateKit;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class DingOaService {
    @Autowired
    AccessTokenService2 accessTokenService;


    /**
     * 获取钉钉调岗记录
     * 获取审批实例id
     *  PsLDOHNLTJChz5s49IH9Pg01701745893083
     */
    public JSONObject getProcessListIds(String processCode){
        JSONObject json=new JSONObject();
        String accessToken = accessTokenService.getAccessToken();

        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/listids");
        OapiProcessinstanceListidsRequest req = new OapiProcessinstanceListidsRequest();
        req.setProcessCode(processCode);//调岗申请 id 通过审查元素 /web/query/process/getProcessVisible.json 接口传参获取
//        req.setStartTime(1738339200000L);//   2025-02-01
//        req.setEndTime(1746028800000L);//     2025-05-01
        //当月的开始时间
        String start = DateKit.getThisMonthStartDate();
        Date start_date = DateKit.dateFormat(start + " 00:00:00");

        String end = DateKit.getThisMonthEndDate();
        Date end_date = DateKit.dateFormat(start + " 23:59:59");
        req.setStartTime(start_date.getTime());//   2025-02-01
        req.setEndTime(end_date.getTime());//     2025-05-01

        req.setSize(10L);
        req.setCursor(0L);
//        req.setUseridList("manager1,manager2");
        OapiProcessinstanceListidsResponse rsp = null;

        try {
            rsp = client.execute(req, accessToken);
            System.out.println(rsp.getBody());
            JSONObject result=JSONObject.parseObject(rsp.getBody());

            if("0".equals(result.getString("errcode"))){
                json=JSONObject.parseObject(result.getString("result"));
            }else{
                throw new YmException(result.getString("errmsg"));
            }
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }

        return json;
    }



    public JSONObject getProcessDetail(String processInstanceId){
        JSONObject json=new JSONObject();
        String accessToken = accessTokenService.getAccessToken();

        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/get");
        OapiProcessinstanceGetRequest req = new OapiProcessinstanceGetRequest();
        req.setProcessInstanceId(processInstanceId);
        OapiProcessinstanceGetResponse rsp = null;
        try {
            rsp = client.execute(req, accessToken);
            System.out.println(rsp.getBody());
            JSONObject result=JSONObject.parseObject(rsp.getBody());

            if("0".equals(result.getString("errcode"))){
                json=JSONObject.parseObject(result.getString("process_instance"));
            }else{
                throw new YmException(result.getString("errmsg"));
            }
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
        return json;
    }


}
